package com.scs.application.modules.fsd.service.impl;

import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.fsd.entity.FsdRecord;
import com.scs.application.modules.fsd.mapper.RecordMapper;
import com.scs.application.modules.fsd.service.RecordService;
import com.scs.application.modules.sys.security.UserPrincipal;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class RecordServiceImpl extends BaseServiceImpl<RecordMapper,FsdRecord> implements RecordService {

    @Override
    public Boolean saveRecord(String stationCode,String changeTime, Integer changeType, List<String> rfids) {
        UserPrincipal currentUser = UserUtils.currentUser();
        Collection<FsdRecord> records = new ArrayList<>();
        for (String rfid : rfids){
            if(StringUtils.isBlank(rfid)) continue;
            FsdRecord fsdRecord = new FsdRecord();
            fsdRecord.setUserId(currentUser.getId());
            fsdRecord.setStationCode(stationCode);
            fsdRecord.setRfid(rfid);
            fsdRecord.setChangeType(changeType);
            fsdRecord.setChangeTime(changeTime);
            records.add(fsdRecord);
        }
        saveBatch(records);
        return true;
    }

    @Override
    public List<Map> getRecordByStationCode(String stationCode) {
        return baseMapper.getRecordByStationCode(stationCode);
    }

    @Override
    public List<Map> getLastScanRecordByStationCode(String stationCode) {
        return baseMapper.getLastScanRecordByStationCode(stationCode);
    }
}
